<?php

/**
 * This is the model class for table "invoices".
 *
 * The followings are the available columns in table 'invoices':
 * @property integer $invoice_id
 * @property integer $model
 * @property integer $model_id
 * @property integer $status
 * @property string $payment_method
 * @property string $payment_code
 * @property string $token
 * @property double $total
 * @property integer $created
 * @property integer $updated
 * @property string $comment
 * @property string $params
 * @property integer $created_by
 * @property integer $updated_by
 */
class Invoice extends CActiveRecord {

    const STATUS_PENDING = 1;
    const STATUS_APPROVE = 2;
    const STATUS_CANCEL = 3;
    const STATUS_DENY = 4;


    //define model
    const MODEL_USER = 1;

    public $title;
    public $userName;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Invoice the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{invoices}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
	// NOTE: you should only define rules for those attributes that
	// will receive user inputs.
	return array(
	    array('model, model_id, status, total', 'required'),
	    array('model, model_id, status, created, updated, created_by, updated_by', 'numerical', 'integerOnly' => true),
	    array('total', 'numerical'),
	    array('payment_method, payment_code', 'length', 'max' => 25),
	    array('comment, params, token', 'safe'),
	    // The following rule is used by search().
	    // Please remove those attributes that should not be searched.
	    array('title,userName,invoice_id, model, model_id, status, payment_method, payment_code, total, created, updated, comment, params, created_by, updated_by', 'safe', 'on' => 'search'),
	);
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.
	return array(
	    'user' => array(self::BELONGS_TO, 'User', 'model_id')
	);
    }

    public function beforeSave() {
	if (parent::beforeSave()) {
	    if ($this->isNewRecord) {
		$this->created = $this->updated = time();
	    } else {
		$this->updated = time();
	    }

	    return true;
	}

	return false;
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'invoice_id' => 'Invoice',
	    'model' => 'Model',
	    'model_id' => 'Model',
	    'status' => 'Status',
	    'payment_method' => 'Payment Method',
	    'payment_code' => 'Payment Code',
	    'total' => 'Total',
	    'created' => 'Created',
	    'updated' => 'Updated',
	    'comment' => 'Comment',
	    'params' => 'Params',
	    'created_by' => 'Created By',
	    'updated_by' => 'Updated By',
	);
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
	// Warning: Please modify the following code to remove attributes that
	// should not be searched.

	$criteria = new CDbCriteria;
	$criteria->with = 'user';
	$criteria->compare('user.username', $this->userName, TRUE);
	$criteria->compare('payment_code', $this->payment_code, true);
	$criteria->compare('t.model', $this->model, TRUE);
	$criteria->order = 'invoice_id DESC';
	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	    'pagination' => array(
		'pageSize' => param('pageSize')
	    ),
	    'sort' => array(
		'defaultOrder' => array(
		    'invoice_id' => CSort::SORT_DESC
		)
	    )
	));
    }

    /**
     * 
     */
    public static function getListTypeModel() {
	return array(
	    self::MODEL_AD => 'Ads',
	    self::MODEL_EVENT => "Event"
	);
    }

}

